package com.misakubex;

import com.hgh.model.EnemyTank;
import com.hgh.model.Map;
import com.hgh.model.Missile;
import com.hgh.util.GenerateEnemyTank;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.concurrent.*;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class BTFrame extends JFrame implements DocumentListener,Runnable {
	/**
	 * 线程池
	 */
	public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(30, Integer.MAX_VALUE, 200, TimeUnit.MILLISECONDS,
			new ArrayBlockingQueue<Runnable>(1024));

	/**
	 * 游戏是否失败结束
	 */
	public static boolean isFail=false;

	/**
	 * 游戏是否成功结束
	 */
	public static boolean isWin=false;

	/**
	 * 地图
	 */
	Map map = new Map();

	/**
	 * 子弹列表
	 */
	public static CopyOnWriteArrayList<Missile> missileList = new CopyOnWriteArrayList<Missile>();

    /**
     * 敌机坦克列表1
     */
    public static CopyOnWriteArrayList<EnemyTank> enemyTankList1 = new CopyOnWriteArrayList<EnemyTank>();

    /**
     * 敌机坦克列表2
     */
    public static CopyOnWriteArrayList<EnemyTank> enemyTankList2 = new CopyOnWriteArrayList<EnemyTank>();

    /**
     * 敌机坦克列表3,三个出生点所以有三个列表
     */
    public static CopyOnWriteArrayList<EnemyTank> enemyTankList3 = new CopyOnWriteArrayList<EnemyTank>();

	public static TankLocal tLocal = new TankLocal();

	private static final long serialVersionUID = 1L;
	JMenuBar menubar=new JMenuBar();
	JMenu menu1=new JMenu("File");
	JMenu menu2=new JMenu("Size");
	JMenu menu3=new JMenu("Help");
	MyPanel btpanel=new MyPanel();
	String [] str1={"restart","key","exit"};
	String [] str2={"tiny","middle","large"};
	String [] str3={"documentation","about"};
	BTFrame(){
		setTitle("BattleTank");
		setSize(Main.size,Main.size);
		setLocation(Main.location,0);
		setJMenuBar(menubar);
		setBackground(Color.blue);
		getContentPane().setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		menubar.add(menu1);
		menubar.add(menu2);
		menubar.add(menu3);
		getContentPane().add(btpanel);
		for(int i=0;i<str1.length;i++){
			JMenuItem item1= new JMenuItem(str1[i]);
			item1.addActionListener(new MyActionListener1());
			menu1.add(item1);
		}
		for(int i=0;i<str2.length;i++){
			JMenuItem item2= new JMenuItem(str2[i]);
			item2.addActionListener(new MyActionListener1());
			menu2.add(item2);
		}
		for(int i=0;i<str3.length;i++){
			JMenuItem item3= new JMenuItem(str3[i]);
			item3.addActionListener(new MyActionListener1());
			menu3.add(item3);
		}
		addKeyListener(tLocal.new KeyMoniter());
		threadPoolExecutor.execute(new GenerateEnemyTank(1));
		threadPoolExecutor.execute(new GenerateEnemyTank(2));
		threadPoolExecutor.execute(new GenerateEnemyTank(3));
		map.initMap();
	}

	/**
	 * 添加新发射的子弹到子弹列表
	 * @param missile
	 */
	public static void addMissile(Missile missile){
		missileList.add(missile);
	}

	/**
	 * 从子弹列表移除子弹
	 * @param missile
	 */
	public static void removeMissile(Missile missile){
		missileList.remove(missile);

	}

	public static void removeTank(int index,EnemyTank enemyTank){
	    switch (index){
            case 1:enemyTankList1.remove(enemyTank);break ;
            case 2:enemyTankList2.remove(enemyTank);break ;
            case 3:enemyTankList3.remove(enemyTank);break ;
            default:break;
        }
    }

	class MyActionListener1 implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()instanceof JMenuItem){
				if(e.getActionCommand()=="restart"){
					System.out.println("1");;
				}
				if(e.getActionCommand()=="key"){
					System.out.println("2");;
				}
				if(e.getActionCommand()=="exit"){
					System.out.println("3");;
				}
				if(e.getActionCommand()=="tiny"){
					System.out.println("4");;
				}
				if(e.getActionCommand()=="middle"){
					System.out.println("5");;
				}
				if(e.getActionCommand()=="large"){
					System.out.println("6");;
				}
				if(e.getActionCommand()=="documentation"){
					System.out.println("7");;
				}
				if(e.getActionCommand()=="about"){
					System.out.println("8");;
				}
			}
		}
	}
	@Override
	public void changedUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub

	}
	@Override
	public void insertUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub

	}
	@Override
	public void removeUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub

	}

	class MyPanel extends JPanel{

		Image picture=null;
		Image realWall=null;
		Image whileWall=null;
		Image gameOver=null;
		Image win = null;
		@Override
		protected void paintComponent(Graphics g) {
			 if(isWin){
				try {
					win=ImageIO.read(new File(Class.class.getClass().getResource("/").getPath() +"image/win.jpg"));
				} catch (IOException e) {
					e.printStackTrace();
				}
				g.drawImage(win, 1*Main.a, 6*Main.a, 24*Main.a, 14*Main.a ,null);
			}
			else if(isFail){
				try {
					gameOver=ImageIO.read(new File(Class.class.getClass().getResource("/").getPath() +"image/gameover.jpg"));
				} catch (IOException e) {
					e.printStackTrace();
				}
				g.drawImage(gameOver, 1*Main.a, 6*Main.a, 24*Main.a, 14*Main.a ,null);
			}
			else{
			// TODO Auto-generated method stub
			super.paintComponent(g);
			g.setColor(Color.black);
			g.fillRect(0, 0, Main.screenWidth, Main.screenHeight);
			g.setColor(Color.green);
			g.fillRect(0, 0, Main.a*26, Main.a*26);
			Graphics2D g2=(Graphics2D)g;
			g2.setColor(Color.red);
			g2.fillRect(tLocal.ta.getX(), tLocal.ta.getY(), tLocal.ta.size, tLocal.ta.size);
			try {
				picture=ImageIO.read(new File(Class.class.getClass().getResource("/").getPath() +"image/tankgreen.jpg"));
				g.drawImage(picture,tLocal.ta.getX() , tLocal.ta.getY(), tLocal.ta.size, tLocal.ta.size ,null);
				for(Missile missile : missileList){
					g.drawImage(picture,missile.getX()+Main.a/2 ,missile.getY()+Main.a/2,Missile.size,Missile.size ,null);
				}
				for(EnemyTank enemyTank : enemyTankList1){
                    g.drawImage(picture,enemyTank.getX() , enemyTank.getY(), enemyTank.size, enemyTank.size ,null);
                }
                for(EnemyTank enemyTank : enemyTankList2){
                    g.drawImage(picture,enemyTank.getX() , enemyTank.getY(), enemyTank.size, enemyTank.size ,null);
                }
                for(EnemyTank enemyTank : enemyTankList3){
                    g.drawImage(picture,enemyTank.getX() , enemyTank.getY(), enemyTank.size, enemyTank.size ,null);
                }
				picture = ImageIO.read(new File(Class.class.getClass().getResource("/").getPath() +"image/glade.jpg"));
				g.drawImage(picture, Map.base[0]*Main.a, Map.base[1]*Main.a, Main.a, Main.a ,null);
				realWall = ImageIO.read(new File(Class.class.getClass().getResource("/").getPath() +"image/brownwall.jpg"));
				whileWall = ImageIO.read(new File(Class.class.getClass().getResource("/").getPath() +"image/whitewall.jpg"));
				for(int i=0;i<Map.HEIGHT;i++){
					for(int j=0;j<Map.WIDTH;j++){
						if(Map.points[i][j]==Map.REDWALL) {
							g.drawImage(realWall, i * Main.a, j * Main.a, Main.a, Main.a, null);
						}
						else if(Map.points[i][j]==Map.WHITEWALL){
							g.drawImage(whileWall, i * Main.a, j * Main.a, Main.a, Main.a, null);
						}
					}
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
	}



	@Override
	public void run() {
		while(true){
			btpanel.repaint();
			try{
				Thread.sleep(10);
			}
			catch (Exception e){
				// TODO: handle exception
			}
		}
	}
}